import 'dart:async';
import 'dart:convert';

import 'package:riki_base_flutter/utils/index.dart';
import 'package:riki_login/entity/staff_login_entity.dart';
import 'package:riki_login/entity/user_login_entity.dart';

import '../riki_login.dart';



class RikiLoginGlobal {

  /// 储存用户资料
  static void saveUserAccount(UserLoginEntity? accountBean) {
    SpUtil.putString(RikiLogin.storageLoginUserInfoKey, accountBean == null ? '' : jsonEncode(accountBean));
  }

  /// 获取用户登录的信息
  static UserLoginEntity? getLocalUserAccount() {
    var userInfo = SpUtil.getString(RikiLogin.storageLoginUserInfoKey);
    if (!TextUtil.isEmpty(userInfo)) {
      try {
        return UserLoginEntity.fromJson(jsonDecode(userInfo));
      } catch (e) {
        print(e);
      }
    }
    return null;
  }

  /// 判断用户User是否登录
  static bool isLoginUserAccount() {
    var id = getLocalUserAccount()?.id ?? '';
    return !TextUtil.isEmpty(id.toString());
  }

  /// 判断用户Staff是否登录
  static bool isLoginStaffAccount() {
    var id = getLocalStaffAccount()?.id ?? '';
    return !TextUtil.isEmpty(id.toString());
  }


  /// 储存用户资料
  static void saveStaffAccount(StaffLoginEntity? accountBean) {
    SpUtil.putString(RikiLogin.storageLoginStaffInfoKey, accountBean == null ? '' : jsonEncode(accountBean));
  }

  /// 获取用户登录的信息
  static StaffLoginEntity? getLocalStaffAccount() {
    var userInfo = SpUtil.getString(RikiLogin.storageLoginStaffInfoKey);
    if (!TextUtil.isEmpty(userInfo)) {
      try {
        return StaffLoginEntity.fromJson(jsonDecode(userInfo));
      } catch (e) {
        print(e);
      }
    }
    return null;
  }


  /// 修改用户中的accessToken
  static updateUserAccessToken(String accessToken) {
    if (!TextUtil.isEmpty(accessToken)) {
      var userInfo = getLocalUserAccount();
      if (userInfo != null) {
        userInfo.accessToken = accessToken;
        saveUserAccount(userInfo);
      }
    }
  }

  /// 修改用户中的 userSig
  static updateUserSig(String userSig) {
    if (!TextUtil.isEmpty(userSig)) {
      var userInfo = getLocalUserAccount();
      if (userInfo != null) {
        userInfo.userSign = userSig;
        saveUserAccount(userInfo);
      }
    }
  }


  /// 是否同意勾选协议
  static bool isUserAgreeAgreement() {
    bool isArgee = SpUtil.getBool(RikiLogin.storageUserAgreeAgreementKey);
    return isArgee;
  }

  /// 设置是否同意勾选协议
  static void setUserAgreeAgreement(bool isAgree) {
    SpUtil.putBool(RikiLogin.storageUserAgreeAgreementKey, isAgree);
  }

  /// 是否同意勾选协议
  static bool isStaffAgreeAgreement() {
    bool isArgee = SpUtil.getBool(RikiLogin.storageStaffAgreeAgreementKey);
    return isArgee;
  }

  /// 设置是否同意勾选协议
  static void setStaffAgreeAgreement(bool isAgree) {
    SpUtil.putBool(RikiLogin.storageStaffAgreeAgreementKey, isAgree);
  }




}
